package com.walterlife.javacore;

import java.lang.Thread.UncaughtExceptionHandler;

public class ExceptionHandler {
    public static void main (String [] args) {
        Thread task = new Thread(new Task(), "task_one");
        task.start();
        String a;
    }
}

class SelfExceptionHandler implements UncaughtExceptionHandler {
    public void uncaughtException(Thread t, Throwable e) {
        System.out.printf("An exception has been captured\n");
        System.out.printf("Thread: %s\n", t.getId());
        System.out.printf("Exception: %s: %s\n", e.getClass().getName(), e.getMessage());
        System.out.printf("Stack Trace: \n");
        e.printStackTrace(System.out);
        System.out.printf("Thread status: %s\n", t.getState());
        new Thread(new Task(), "task_one").start();
    } 
} 

class Task implements Runnable {
    public void run() {
        Thread.currentThread().setUncaughtExceptionHandler(new SelfExceptionHandler());
        System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.parseInt("xyz"));
        System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.parseInt("123"));
    } 
} 
